package com.bc004346.travelus.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.bc004346.travelus.Adapter.FlightAdapter;
import com.bc004346.travelus.FlightPagerActivity;
import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.Model.Domain.Reservation;
import com.bc004346.travelus.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.bc004346.travelus.Fragment.FlightDetailsFragment.EXTRA_FLIGHTS;
import static com.bc004346.travelus.Fragment.FlightDetailsFragment.EXTRA_FLIGHT_ID;
import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by Renats on 9/4/2014.
 * The new flight list
 */
public class FlightListFragment extends ListFragment {

    private static final String KEY_FLIGHT = "com.bc004346FlightListFragment.Flight";
    private ArrayList<Flight> mFlights = new ArrayList<Flight>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        if (savedInstanceState != null)
            //noinspection unchecked
            mFlights = (ArrayList<Flight>) savedInstanceState.getSerializable(KEY_FLIGHT);
        FlightAdapter adapter = new FlightAdapter(getActivity(), mFlights);
        setListAdapter(adapter);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        try {
            outState.putSerializable(KEY_FLIGHT, mFlights);
        } catch (Exception e) {
            log(e, getActivity());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.flight_selector, container, false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        try {
            Intent i = new Intent(getActivity(), FlightPagerActivity.class);
            i.putExtra(EXTRA_FLIGHT_ID, position);
            i.putExtra(EXTRA_FLIGHTS, mFlights);
            startActivity(i);
        } catch (ClassCastException e) {
            Toast.makeText(getActivity(),
                    String.format("You do no have any flights within the number of days specified in settings"),
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            log(e, getActivity());
        }
    }

    public void refresh(List<Reservation> reservations) {

        try {
            mFlights.clear();
            final ListView lv;
            lv = getListView();
            if (reservations.size() > 0) {
                for (Reservation res : reservations)
                    for (Flight flight : res.getFlights())
                        mFlights.add(flight);

                Collections.sort(mFlights);

                FlightAdapter adapter = new FlightAdapter(getActivity(), mFlights);
                setListAdapter(adapter);

                if (lv != null) {
                    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    lv.setSelector(android.R.color.darker_gray);
                }

            } else {
                ArrayAdapter<String> flightArrayAdapter = new ArrayAdapter<String>(getActivity(),
                        R.layout.list_flight_simple, new ArrayList<String>() {{
                    add(getActivity().getString(R.string.common_no_flights_found));
                }}
                );
                setListAdapter(flightArrayAdapter);
                if (lv != null)
                    lv.setChoiceMode(ListView.CHOICE_MODE_NONE);

            }
        } catch (IllegalStateException e) {
            log(e.getMessage());
        } catch (Exception e) {
            log(e, getActivity());
        }
    }

    public void refreshFlights(List<Flight> flights) {

        try {
            mFlights.clear();
            final ListView lv = getListView();
            mFlights.addAll(flights);
            if (flights.size() > 0) {

                Collections.sort(mFlights);

                FlightAdapter adapter = new FlightAdapter(getActivity(), mFlights);
                setListAdapter(adapter);

                if (lv != null) {
                    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    lv.setSelector(android.R.color.darker_gray);
                }

            } else {
                ArrayAdapter<String> flightArrayAdapter = new ArrayAdapter<String>(getActivity(),
                        R.layout.list_flight_simple, new ArrayList<String>() {{
                    add(getActivity().getString(R.string.common_no_flights_found));
                }}
                );
                setListAdapter(flightArrayAdapter);
                if (lv != null)
                    lv.setChoiceMode(ListView.CHOICE_MODE_NONE);

            }
        } catch (Exception e) {
            log(e, getActivity());
        }
    }
}
